<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Optical Flow</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d3/dd5/tutorial_table_of_content_other.html">Other tutorials (ml, objdetect, photo, stitching, video)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Optical Flow </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d7/d00/tutorial_meanshift.html">Meanshift and Camshift</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../db/d28/tutorial_cascade_classifier.html">Cascade Classifier</a></p>
<h2>Goal </h2>
<p>In this chapter,</p><ul>
<li>We will understand the concepts of optical flow and its estimation using Lucas-Kanade method.</li>
<li>We will use functions like <b><a class="el" href="../../dc/d6b/group__video__track.html#ga473e4b886d0bcc6b65831eb88ed93323" title="Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyra...">cv.calcOpticalFlowPyrLK()</a></b> to track feature points in a video.</li>
<li>We will create a dense optical flow field using the <b><a class="el" href="../../dc/d6b/group__video__track.html#ga5d10ebbd59fe09c5f650289ec0ece5af" title="Computes a dense optical flow using the Gunnar Farneback&#39;s algorithm. ">cv.calcOpticalFlowFarneback()</a></b> method.</li>
</ul>
<h2>Optical Flow </h2>
<p>Optical flow is the pattern of apparent motion of image objects between two consecutive frames caused by the movement of object or camera. It is 2D vector field where each vector is a displacement vector showing the movement of points from first frame to second. Consider the image below (Image Courtesy: <a href="http://en.wikipedia.org/wiki/Optical_flow">Wikipedia article on Optical Flow</a>).</p>
<div class="image">
<img src="../../optical_flow_basic1.jpg" alt="optical_flow_basic1.jpg"/>
<div class="caption">
image</div></div>
<p> It shows a ball moving in 5 consecutive frames. The arrow shows its displacement vector. Optical flow has many applications in areas like :</p>
<ul>
<li>Structure from Motion</li>
<li>Video Compression</li>
<li>Video Stabilization ...</li>
</ul>
<p>Optical flow works on several assumptions:</p>
<ol type="1">
<li>The pixel intensities of an object do not change between consecutive frames.</li>
<li>Neighbouring pixels have similar motion.</li>
</ol>
<p>Consider a pixel \(I(x,y,t)\) in first frame (Check a new dimension, time, is added here. Earlier we were working with images only, so no need of time). It moves by distance \((dx,dy)\) in next frame taken after \(dt\) time. So since those pixels are the same and intensity does not change, we can say,</p>
<p class="formulaDsp">
\[I(x,y,t) = I(x+dx, y+dy, t+dt)\]
</p>
<p>Then take taylor series approximation of right-hand side, remove common terms and divide by \(dt\) to get the following equation:</p>
<p class="formulaDsp">
\[f_x u + f_y v + f_t = 0 \;\]
</p>
<p>where:</p>
<p class="formulaDsp">
\[f_x = \frac{\partial f}{\partial x} \; ; \; f_y = \frac{\partial f}{\partial y}\]
</p>
 <p class="formulaDsp">
\[u = \frac{dx}{dt} \; ; \; v = \frac{dy}{dt}\]
</p>
<p>Above equation is called Optical Flow equation. In it, we can find \(f_x\) and \(f_y\), they are image gradients. Similarly \(f_t\) is the gradient along time. But \((u,v)\) is unknown. We cannot solve this one equation with two unknown variables. So several methods are provided to solve this problem and one of them is Lucas-Kanade.</p>
<h3>Lucas-Kanade method</h3>
<p>We have seen an assumption before, that all the neighbouring pixels will have similar motion. Lucas-Kanade method takes a 3x3 patch around the point. So all the 9 points have the same motion. We can find \((f_x, f_y, f_t)\) for these 9 points. So now our problem becomes solving 9 equations with two unknown variables which is over-determined. A better solution is obtained with least square fit method. Below is the final solution which is two equation-two unknown problem and solve to get the solution.</p>
<p class="formulaDsp">
\[\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} \sum_{i}{f_{x_i}}^2 &amp; \sum_{i}{f_{x_i} f_{y_i} } \\ \sum_{i}{f_{x_i} f_{y_i}} &amp; \sum_{i}{f_{y_i}}^2 \end{bmatrix}^{-1} \begin{bmatrix} - \sum_{i}{f_{x_i} f_{t_i}} \\ - \sum_{i}{f_{y_i} f_{t_i}} \end{bmatrix}\]
</p>
<p>( Check similarity of inverse matrix with Harris corner detector. It denotes that corners are better points to be tracked.)</p>
<p>So from the user point of view, the idea is simple, we give some points to track, we receive the optical flow vectors of those points. But again there are some problems. Until now, we were dealing with small motions, so it fails when there is a large motion. To deal with this we use pyramids. When we go up in the pyramid, small motions are removed and large motions become small motions. So by applying Lucas-Kanade there, we get optical flow along with the scale.</p>
<h2>Lucas-Kanade Optical Flow in OpenCV </h2>
<p>OpenCV provides all these in a single function, <b><a class="el" href="../../dc/d6b/group__video__track.html#ga473e4b886d0bcc6b65831eb88ed93323" title="Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyra...">cv.calcOpticalFlowPyrLK()</a></b>. Here, we create a simple application which tracks some points in a video. To decide the points, we use <b><a class="el" href="../../dd/d1a/group__imgproc__feature.html#ga1d6bb77486c8f92d79c8793ad995d541" title="Determines strong corners on an image. ">cv.goodFeaturesToTrack()</a></b>. We take the first frame, detect some Shi-Tomasi corner points in it, then we iteratively track those points using Lucas-Kanade optical flow. For the function <b><a class="el" href="../../dc/d6b/group__video__track.html#ga473e4b886d0bcc6b65831eb88ed93323" title="Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyra...">cv.calcOpticalFlowPyrLK()</a></b> we pass the previous frame, previous points and next frame. It returns next points along with some status numbers which has a value of 1 if next point is found, else zero. We iteratively pass these next points as previous points in next step. See the code below:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/optical_flow/optical_flow.cpp">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d1c/video_2include_2opencv2_2video_8hpp.html">opencv2/video.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> about =</div><div class="line">        <span class="stringliteral">&quot;This sample demonstrates Lucas-Kanade Optical Flow calculation.\n&quot;</span></div><div class="line">        <span class="stringliteral">&quot;The example file can be downloaded from:\n&quot;</span></div><div class="line">        <span class="stringliteral">&quot;  https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4&quot;</span>;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> keys =</div><div class="line">        <span class="stringliteral">&quot;{ h help |      | print this help message }&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{ @image | vtest.avi | path to image file }&quot;</span>;</div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser(argc, argv, keys);</div><div class="line">    parser.about(about);</div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <span class="keywordtype">string</span> filename = <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(parser.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;@image&quot;</span>));</div><div class="line">    <span class="keywordflow">if</span> (!parser.check())</div><div class="line">    {</div><div class="line">        parser.printErrors();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> capture(filename);</div><div class="line">    <span class="keywordflow">if</span> (!capture.isOpened()){</div><div class="line">        <span class="comment">//error in opening the video input</span></div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;Unable to open file!&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Create some random colors</span></div><div class="line">    vector&lt;Scalar&gt; colors;</div><div class="line">    <a class="code" href="../../d1/dd6/classcv_1_1RNG.html">RNG</a> rng;</div><div class="line">    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i &lt; 100; i++)</div><div class="line">    {</div><div class="line">        <span class="keywordtype">int</span> r = rng.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#acde197860cea91e5aa896be8719457ae">uniform</a>(0, 256);</div><div class="line">        <span class="keywordtype">int</span> g = rng.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#acde197860cea91e5aa896be8719457ae">uniform</a>(0, 256);</div><div class="line">        <span class="keywordtype">int</span> b = rng.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#acde197860cea91e5aa896be8719457ae">uniform</a>(0, 256);</div><div class="line">        colors.push_back(<a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(r,g,b));</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> old_frame, old_gray;</div><div class="line">    vector&lt;Point2f&gt; p0, p1;</div><div class="line"></div><div class="line">    <span class="comment">// Take first frame and find corners in it</span></div><div class="line">    capture &gt;&gt; old_frame;</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(old_frame, old_gray, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a>);</div><div class="line">    <a class="code" href="../../dd/d1a/group__imgproc__feature.html#ga1d6bb77486c8f92d79c8793ad995d541">goodFeaturesToTrack</a>(old_gray, p0, 100, 0.3, 7, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), 7, <span class="keyword">false</span>, 0.04);</div><div class="line"></div><div class="line">    <span class="comment">// Create a mask image for drawing purposes</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> mask = <a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">Mat::zeros</a>(old_frame.size(), old_frame.type());</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span>(<span class="keyword">true</span>){</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame, frame_gray;</div><div class="line"></div><div class="line">        capture &gt;&gt; frame;</div><div class="line">        <span class="keywordflow">if</span> (frame.empty())</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(frame, frame_gray, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a>);</div><div class="line"></div><div class="line">        <span class="comment">// calculate optical flow</span></div><div class="line">        vector&lt;uchar&gt; status;</div><div class="line">        vector&lt;float&gt; err;</div><div class="line">        <a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html">TermCriteria</a> criteria = <a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html">TermCriteria</a>((<a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57aeb9da694ea67b3ef7d524521b580867d">TermCriteria::COUNT</a>) + (<a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57a857609e73e7028e638d2ea649f3b45d5">TermCriteria::EPS</a>), 10, 0.03);</div><div class="line">        <a class="code" href="../../dc/d6b/group__video__track.html#ga473e4b886d0bcc6b65831eb88ed93323">calcOpticalFlowPyrLK</a>(old_gray, frame_gray, p0, p1, status, err, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(15,15), 2, criteria);</div><div class="line"></div><div class="line">        vector&lt;Point2f&gt; good_new;</div><div class="line">        <span class="keywordflow">for</span>(<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4f5fce8c1ef282264f9214809524d836">uint</a> i = 0; i &lt; p0.size(); i++)</div><div class="line">        {</div><div class="line">            <span class="comment">// Select good points</span></div><div class="line">            <span class="keywordflow">if</span>(status[i] == 1) {</div><div class="line">                good_new.push_back(p1[i]);</div><div class="line">                <span class="comment">// draw the tracks</span></div><div class="line">                <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2">line</a>(mask,p1[i], p0[i], colors[i], 2);</div><div class="line">                <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gaf10604b069374903dbd0f0488cb43670">circle</a>(frame, p1[i], 5, colors[i], -1);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img;</div><div class="line">        <a class="code" href="../../d2/de8/group__core__array.html#ga10ac1bfb180e2cfda1701d06c24fdbd6">add</a>(frame, mask, img);</div><div class="line"></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;Frame&quot;</span>, img);</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> keyboard = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">        <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span> || keyboard == 27)</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">        <span class="comment">// Now update the previous frame and previous points</span></div><div class="line">        old_gray = frame_gray.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">clone</a>();</div><div class="line">        p0 = good_new;</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/video/optical_flow/optical_flow.py">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;This sample demonstrates Lucas-Kanade Optical Flow calculation. \</span></div><div class="line"><span class="stringliteral">                                              The example file can be downloaded from: \</span></div><div class="line"><span class="stringliteral">                                              https://www.bogotobogo.com/python/OpenCV_Python/images/mean_shift_tracking/slow_traffic_small.mp4&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;image&#39;</span>, type=str, help=<span class="stringliteral">&#39;path to image file&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line">cap = <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">cv.VideoCapture</a>(args.image)</div><div class="line"></div><div class="line"><span class="comment"># params for ShiTomasi corner detection</span></div><div class="line">feature_params = dict( maxCorners = 100,</div><div class="line">                       qualityLevel = 0.3,</div><div class="line">                       minDistance = 7,</div><div class="line">                       blockSize = 7 )</div><div class="line"></div><div class="line"><span class="comment"># Parameters for lucas kanade optical flow</span></div><div class="line">lk_params = dict( winSize  = (15,15),</div><div class="line">                  maxLevel = 2,</div><div class="line">                  criteria = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))</div><div class="line"></div><div class="line"><span class="comment"># Create some random colors</span></div><div class="line">color = np.random.randint(0,255,(100,3))</div><div class="line"></div><div class="line"><span class="comment"># Take first frame and find corners in it</span></div><div class="line">ret, old_frame = cap.read()</div><div class="line">old_gray = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(old_frame, cv.COLOR_BGR2GRAY)</div><div class="line">p0 = <a class="code" href="../../dd/d1a/group__imgproc__feature.html#gac52aa0fc91b1fd4a5f5a8c7d80e04bd4">cv.goodFeaturesToTrack</a>(old_gray, mask = <span class="keywordtype">None</span>, **feature_params)</div><div class="line"></div><div class="line"><span class="comment"># Create a mask image for drawing purposes</span></div><div class="line">mask = np.zeros_like(old_frame)</div><div class="line"></div><div class="line">while(1):</div><div class="line">    ret,frame = cap.read()</div><div class="line">    frame_gray = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(frame, cv.COLOR_BGR2GRAY)</div><div class="line"></div><div class="line">    <span class="comment"># calculate optical flow</span></div><div class="line">    p1, st, err = <a class="code" href="../../dc/d6b/group__video__track.html#ga473e4b886d0bcc6b65831eb88ed93323">cv.calcOpticalFlowPyrLK</a>(old_gray, frame_gray, p0, <span class="keywordtype">None</span>, **lk_params)</div><div class="line"></div><div class="line">    <span class="comment"># Select good points</span></div><div class="line">    <span class="keywordflow">if</span> p1 <span class="keywordflow">is</span> <span class="keywordflow">not</span> <span class="keywordtype">None</span>:</div><div class="line">        good_new = p1[st==1]</div><div class="line">        good_old = p0[st==1]</div><div class="line"></div><div class="line">    <span class="comment"># draw the tracks</span></div><div class="line">    <span class="keywordflow">for</span> i,(new,old) <span class="keywordflow">in</span> enumerate(zip(good_new, good_old)):</div><div class="line">        a,b = new.ravel()</div><div class="line">        c,d = old.ravel()</div><div class="line">        mask = <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2">cv.line</a>(mask, (int(a),int(b)),(int(c),int(d)), color[i].tolist(), 2)</div><div class="line">        frame = <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gaf10604b069374903dbd0f0488cb43670">cv.circle</a>(frame,(int(a),int(b)),5,color[i].tolist(),-1)</div><div class="line">    img = <a class="code" href="../../d2/de8/group__core__array.html#ga10ac1bfb180e2cfda1701d06c24fdbd6">cv.add</a>(frame,mask)</div><div class="line"></div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;frame&#39;</span>,img)</div><div class="line">    k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(30) &amp; 0xff</div><div class="line">    <span class="keywordflow">if</span> k == 27:</div><div class="line">        <span class="keywordflow">break</span></div><div class="line"></div><div class="line">    <span class="comment"># Now update the previous frame and previous points</span></div><div class="line">    old_gray = frame_gray.copy()</div><div class="line">    p0 = good_new.reshape(-1,1,2)</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/optical_flow/OpticalFlowDemo.java">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> java.util.ArrayList;</div><div class="line"><span class="keyword">import</span> java.util.Random;</div><div class="line"><span class="keyword">import</span> org.opencv.core.*;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"><span class="keyword">import</span> org.opencv.video.Video;</div><div class="line"><span class="keyword">import</span> org.opencv.videoio.VideoCapture;</div><div class="line"></div><div class="line"><span class="keyword">class </span>OptFlow {</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = args[0];</div><div class="line">        VideoCapture capture = <span class="keyword">new</span> VideoCapture(filename);</div><div class="line">        <span class="keywordflow">if</span> (!capture.isOpened()) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Unable to open this file&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line"></div><div class="line"></div><div class="line">        <span class="comment">// Create some random colors</span></div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>[] colors = <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>[100];</div><div class="line">        Random rng = <span class="keyword">new</span> Random();</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0 ; i &lt; 100 ; i++) {</div><div class="line">            <span class="keywordtype">int</span> r = rng.nextInt(256);</div><div class="line">            <span class="keywordtype">int</span> g = rng.nextInt(256);</div><div class="line">            <span class="keywordtype">int</span> b = rng.nextInt(256);</div><div class="line">            colors[i] = <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(r, g, b);</div><div class="line">        }</div><div class="line"></div><div class="line">        Mat old_frame = <span class="keyword">new</span> Mat() , old_gray = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">        <span class="comment">// Since the function Imgproc.goodFeaturesToTrack requires MatofPoint</span></div><div class="line">        <span class="comment">// therefore first p0MatofPoint is passed to the function and then converted to MatOfPoint2f</span></div><div class="line">        MatOfPoint p0MatofPoint = <span class="keyword">new</span> MatOfPoint();</div><div class="line">        capture.read(old_frame);</div><div class="line">        Imgproc.cvtColor(old_frame, old_gray, Imgproc.COLOR_BGR2GRAY);</div><div class="line">        Imgproc.goodFeaturesToTrack(old_gray, p0MatofPoint,100,0.3,7, <span class="keyword">new</span> Mat(),7,<span class="keyword">false</span>,0.04);</div><div class="line"></div><div class="line">        MatOfPoint2f p0 = <span class="keyword">new</span> MatOfPoint2f(p0MatofPoint.toArray()) , p1 = <span class="keyword">new</span> MatOfPoint2f();</div><div class="line"></div><div class="line">        <span class="comment">// Create a mask image for drawing purposes</span></div><div class="line">        Mat mask = Mat.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">zeros</a>(old_frame.size(), old_frame.type());</div><div class="line"></div><div class="line">        <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line">            Mat frame = <span class="keyword">new</span> Mat(), frame_gray = <span class="keyword">new</span> Mat();</div><div class="line">            capture.read(frame);</div><div class="line">            <span class="keywordflow">if</span> (frame.empty()) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line"></div><div class="line">            Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY);</div><div class="line"></div><div class="line">            <span class="comment">// calculate optical flow</span></div><div class="line">            MatOfByte status = <span class="keyword">new</span> MatOfByte();</div><div class="line">            MatOfFloat err = <span class="keyword">new</span> MatOfFloat();</div><div class="line">            TermCriteria criteria = <span class="keyword">new</span> TermCriteria(TermCriteria.COUNT + TermCriteria.EPS,10,0.03);</div><div class="line">            Video.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, p1, status, err, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(15,15),2, criteria);</div><div class="line"></div><div class="line">            byte StatusArr[] = status.toArray();</div><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a> p0Arr[] = p0.toArray();</div><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a> p1Arr[] = p1.toArray();</div><div class="line">            ArrayList&lt;Point&gt; good_new = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line"></div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i&lt;StatusArr.length ; i++ ) {</div><div class="line">                <span class="keywordflow">if</span> (StatusArr[i] == 1) {</div><div class="line">                    good_new.add(p1Arr[i]);</div><div class="line">                    Imgproc.line(mask, p1Arr[i], p0Arr[i], colors[i],2);</div><div class="line">                    Imgproc.circle(frame, p1Arr[i],5, colors[i],-1);</div><div class="line">                }</div><div class="line">            }</div><div class="line"></div><div class="line">            Mat img = <span class="keyword">new</span> Mat();</div><div class="line">            Core.add(frame, mask, img);</div><div class="line"></div><div class="line">            HighGui.imshow(<span class="stringliteral">&quot;Frame&quot;</span>, img);</div><div class="line"></div><div class="line">            <span class="keywordtype">int</span> keyboard = HighGui.waitKey(30);</div><div class="line">            <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span> || keyboard == 27) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line"></div><div class="line">            <span class="comment">// Now update the previous frame and previous points</span></div><div class="line">            old_gray = frame_gray.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">clone</a>();</div><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>[] good_new_arr = <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>[good_new.size()];</div><div class="line">            good_new_arr = good_new.toArray(good_new_arr);</div><div class="line">            p0 = <span class="keyword">new</span> MatOfPoint2f(good_new_arr);</div><div class="line">        }</div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>OpticalFlowDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line">        <span class="keyword">new</span> OptFlow().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
<p>(This code doesn't check how correct are the next keypoints. So even if any feature point disappears in image, there is a chance that optical flow finds the next point which may look close to it. So actually for a robust tracking, corner points should be detected in particular intervals. OpenCV samples comes up with such a sample which finds the feature points at every 5 frames. It also run a backward-check of the optical flow points got to select only good ones. Check samples/python/lk_track.py).</p>
<p>See the results we got:</p>
<div class="image">
<img src="../../opticalflow_lk.jpg" alt="opticalflow_lk.jpg"/>
<div class="caption">
image</div></div>
 <h2>Dense Optical Flow in OpenCV </h2>
<p>Lucas-Kanade method computes optical flow for a sparse feature set (in our example, corners detected using Shi-Tomasi algorithm). OpenCV provides another algorithm to find the dense optical flow. It computes the optical flow for all the points in the frame. It is based on Gunner Farneback's algorithm which is explained in "Two-Frame Motion Estimation Based on Polynomial Expansion" by Gunner Farneback in 2003.</p>
<p>Below sample shows how to find the dense optical flow using above algorithm. We get a 2-channel array with optical flow vectors, \((u,v)\). We find their magnitude and direction. We color code the result for better visualization. Direction corresponds to Hue value of the image. Magnitude corresponds to Value plane. See the code below:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/video/optical_flow/optical_flow_dense.cpp">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d1c/video_2include_2opencv2_2video_8hpp.html">opencv2/video.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> capture(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(<span class="stringliteral">&quot;vtest.avi&quot;</span>));</div><div class="line">    <span class="keywordflow">if</span> (!capture.isOpened()){</div><div class="line">        <span class="comment">//error in opening the video input</span></div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;Unable to open file!&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame1, prvs;</div><div class="line">    capture &gt;&gt; frame1;</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(frame1, prvs, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a>);</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span>(<span class="keyword">true</span>){</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame2, next;</div><div class="line">        capture &gt;&gt; frame2;</div><div class="line">        <span class="keywordflow">if</span> (frame2.empty())</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(frame2, next, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a>);</div><div class="line"></div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> flow(prvs.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga15d6109d87682bf909122d0ce51c46a6">CV_32FC2</a>);</div><div class="line">        <a class="code" href="../../dc/d6b/group__video__track.html#ga5d10ebbd59fe09c5f650289ec0ece5af">calcOpticalFlowFarneback</a>(prvs, next, flow, 0.5, 3, 15, 3, 5, 1.2, 0);</div><div class="line"></div><div class="line">        <span class="comment">// visualization</span></div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> flow_parts[2];</div><div class="line">        <a class="code" href="../../d2/de8/group__core__array.html#ga0547c7fed86152d7e9d0096029c8518a">split</a>(flow, flow_parts);</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> <a class="code" href="../../d2/de8/group__core__array.html#ga6d3b097586bca4409873d64a90fe64c3">magnitude</a>, angle, magn_norm;</div><div class="line">        <a class="code" href="../../d2/de8/group__core__array.html#gac5f92f48ec32cacf5275969c33ee837d">cartToPolar</a>(flow_parts[0], flow_parts[1], magnitude, angle, <span class="keyword">true</span>);</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>(magnitude, magn_norm, 0.0f, 1.0f, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>);</div><div class="line">        angle *= ((1.f / 360.f) * (180.f / 255.f));</div><div class="line"></div><div class="line">        <span class="comment">//build hsv image</span></div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> _hsv[3], hsv, hsv8, bgr;</div><div class="line">        _hsv[0] = angle;</div><div class="line">        _hsv[1] = <a class="code" href="../../d3/d63/classcv_1_1Mat.html#a69ae0402d116fc9c71908d8508dc2f09">Mat::ones</a>(angle.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>);</div><div class="line">        _hsv[2] = magn_norm;</div><div class="line">        <a class="code" href="../../d2/de8/group__core__array.html#ga7d7b4d6c6ee504b30a20b1680029c7b4">merge</a>(_hsv, 3, hsv);</div><div class="line">        hsv.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adf88c60c5b4980e05bb556080916978b">convertTo</a>(hsv8, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32b18d904ee2b1731a9416a8eef67d06">CV_8U</a>, 255.0);</div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(hsv8, bgr, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a2a4b11ff7d29c342b66b85962a7969cd">COLOR_HSV2BGR</a>);</div><div class="line"></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;frame2&quot;</span>, bgr);</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> keyboard = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">        <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span> || keyboard == 27)</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">        prvs = next;</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/video/optical_flow/optical_flow_dense.py">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line">cap = <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">cv.VideoCapture</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(<span class="stringliteral">&quot;vtest.avi&quot;</span>))</div><div class="line">ret, frame1 = cap.read()</div><div class="line">prvs = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(frame1,cv.COLOR_BGR2GRAY)</div><div class="line">hsv = np.zeros_like(frame1)</div><div class="line">hsv[...,1] = 255</div><div class="line">while(1):</div><div class="line">    ret, frame2 = cap.read()</div><div class="line">    next = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(frame2,cv.COLOR_BGR2GRAY)</div><div class="line">    flow = <a class="code" href="../../dc/d6b/group__video__track.html#ga5d10ebbd59fe09c5f650289ec0ece5af">cv.calcOpticalFlowFarneback</a>(prvs,next, <span class="keywordtype">None</span>, 0.5, 3, 15, 3, 5, 1.2, 0)</div><div class="line">    mag, ang = <a class="code" href="../../d2/de8/group__core__array.html#gac5f92f48ec32cacf5275969c33ee837d">cv.cartToPolar</a>(flow[...,0], flow[...,1])</div><div class="line">    hsv[...,0] = ang*180/np.pi/2</div><div class="line">    hsv[...,2] = <a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(mag,<span class="keywordtype">None</span>,0,255,cv.NORM_MINMAX)</div><div class="line">    bgr = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(hsv,cv.COLOR_HSV2BGR)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;frame2&#39;</span>,bgr)</div><div class="line">    k = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(30) &amp; 0xff</div><div class="line">    <span class="keywordflow">if</span> k == 27:</div><div class="line">        <span class="keywordflow">break</span></div><div class="line">    <span class="keywordflow">elif</span> k == ord(<span class="stringliteral">&#39;s&#39;</span>):</div><div class="line">        <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;opticalfb.png&#39;</span>,frame2)</div><div class="line">        <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;opticalhsv.png&#39;</span>,bgr)</div><div class="line">    prvs = next</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/video/optical_flow/OpticalFlowDenseDemo.java">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> java.util.ArrayList;</div><div class="line"><span class="keyword">import</span> org.opencv.core.*;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"><span class="keyword">import</span> org.opencv.video.Video;</div><div class="line"><span class="keyword">import</span> org.opencv.videoio.VideoCapture;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">class </span>OptFlowDense {</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = args[0];</div><div class="line">        VideoCapture capture = <span class="keyword">new</span> VideoCapture(filename);</div><div class="line">        <span class="keywordflow">if</span> (!capture.isOpened()) {</div><div class="line">            <span class="comment">//error in opening the video input</span></div><div class="line">            System.out.println(<span class="stringliteral">&quot;Unable to open file!&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line"></div><div class="line">        Mat frame1 = <span class="keyword">new</span> Mat() , prvs = <span class="keyword">new</span> Mat();</div><div class="line">        capture.read(frame1);</div><div class="line">        Imgproc.cvtColor(frame1, prvs, Imgproc.COLOR_BGR2GRAY);</div><div class="line"></div><div class="line">        <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line">            Mat frame2 = <span class="keyword">new</span> Mat(), next = <span class="keyword">new</span> Mat();</div><div class="line">            capture.read(frame2);</div><div class="line">            <span class="keywordflow">if</span> (frame2.empty()) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">            Imgproc.cvtColor(frame2, next, Imgproc.COLOR_BGR2GRAY);</div><div class="line"></div><div class="line">            Mat flow = <span class="keyword">new</span> Mat(prvs.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), CvType.CV_32FC2);</div><div class="line">            Video.calcOpticalFlowFarneback(prvs, next, flow,0.5,3,15,3,5,1.2,0);</div><div class="line"></div><div class="line">            <span class="comment">// visualization</span></div><div class="line">            ArrayList&lt;Mat&gt; flow_parts = <span class="keyword">new</span> ArrayList&lt;&gt;(2);</div><div class="line">            Core.split(flow, flow_parts);</div><div class="line">            Mat magnitude = <span class="keyword">new</span> Mat(), angle = <span class="keyword">new</span> Mat(), magn_norm = <span class="keyword">new</span> Mat();</div><div class="line">            Core.cartToPolar(flow_parts.get(0), flow_parts.get(1), <a class="code" href="../../d2/de8/group__core__array.html#ga6d3b097586bca4409873d64a90fe64c3">magnitude</a>, angle,<span class="keyword">true</span>);</div><div class="line">            Core.normalize(magnitude, magn_norm,0.0,1.0, Core.NORM_MINMAX);</div><div class="line">            <span class="keywordtype">float</span> factor = (float) ((1.0/360.0)*(180.0/255.0));</div><div class="line">            Mat new_angle = <span class="keyword">new</span> Mat();</div><div class="line">            Core.multiply(angle, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(factor), new_angle);</div><div class="line"></div><div class="line">            <span class="comment">//build hsv image</span></div><div class="line">            ArrayList&lt;Mat&gt; _hsv = <span class="keyword">new</span> ArrayList&lt;&gt;() ;</div><div class="line">            Mat hsv = <span class="keyword">new</span> Mat(), hsv8 = <span class="keyword">new</span> Mat(), bgr = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">            _hsv.add(new_angle);</div><div class="line">            _hsv.add(Mat.ones(angle.size(), CvType.CV_32F));</div><div class="line">            _hsv.add(magn_norm);</div><div class="line">            Core.merge(_hsv, hsv);</div><div class="line">            hsv.convertTo(hsv8, CvType.CV_8U, 255.0);</div><div class="line">            Imgproc.cvtColor(hsv8, bgr, Imgproc.COLOR_HSV2BGR);</div><div class="line"></div><div class="line">            HighGui.imshow(<span class="stringliteral">&quot;frame2&quot;</span>, bgr);</div><div class="line"></div><div class="line">            <span class="keywordtype">int</span> keyboard = HighGui.waitKey(30);</div><div class="line">            <span class="keywordflow">if</span> (keyboard == <span class="charliteral">&#39;q&#39;</span> || keyboard == 27) {</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">            prvs = next;</div><div class="line">        }</div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>OpticalFlowDenseDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line">        <span class="keyword">new</span> OptFlowDense().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
<p>See the result below:</p>
<div class="image">
<img src="../../opticalfb.jpg" alt="opticalfb.jpg"/>
<div class="caption">
image</div></div>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:37 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
